package com.billionsfinance.crs.model.system;

/**
 * @author wenhai.wang date 2016年5月3日 下午3:34:44
 */
public class GenerateCodeModule implements java.io.Serializable {

	private static final long serialVersionUID = 2977884047245476483L;
	/**
	 * 表名称
	 */
	private String tableName;
	/**
	 * 对应JAVA代码里面名称
	 */
	private String propertyName;
	/**
	 * 数据库字段
	 */
	private String columnName;
	/**
	 * 对应Mybatis的JBDC 类型
	 */
	private String jdbcType;
	/**
	 * 字段描述
	 */
	private String fieldDis;
	/**
	 * 字段首字母大写
	 */
	private String capFieldName;

	/**
	 * 对应JAVA代码里面的类型
	 */
	private String fieldType;

	public String getFieldType() {
		return fieldType;
	}

	public void setFieldType(String fieldType) {
		this.fieldType = getFType(fieldType);
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getPropertyName() {
		return propertyName;
	}

	public void setPropertyName(String propertyName) {
		this.propertyName = getName(propertyName);
	}

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public String getJdbcType() {
		return jdbcType;
	}

	public void setJdbcType(String jdbcType) {
		this.jdbcType = getType(jdbcType);
	}

	public String getFieldDis() {
		return fieldDis;
	}

	public void setFieldDis(String fieldDis) {
		this.fieldDis = fieldDis;
	}

	public String getCapFieldName() {
		return capFieldName;
	}

	public void setCapFieldName(String capFieldName) {
		this.capFieldName = getCapName(capFieldName);
	}

	/**
	 * 将数据库里面的字段转成JAVA代码里面的字段
	 *
	 * @param name
	 * @return String
	 */
	private String getName(String name) {
		String[] names = name.toLowerCase().split("_");
		StringBuffer sb = new StringBuffer();
		if (names.length > 1) {
			sb.append(names[0]);
			for (int i = 1; i <= names.length - 1; i++) {
				sb.append(names[i].substring(0, 1).toUpperCase() + names[i].substring(1));
			}
		} else {
			sb.append(names[0]);
		}
		return sb.toString();
	}

	/**
	 * 处理首字母大写
	 *
	 * @param name
	 * @return String
	 */
	private String getCapName(String name) {
		return name.substring(0, 1).toUpperCase() + name.substring(1);
	}

	/**
	 * 得到对应MyBatis的JDBC类型
	 *
	 * @param type
	 * @return String
	 */
	private String getType(String type) {
		if ("VARCHAR2".equals(type)) {
			return "VARCHAR";
		} else if ("NUMBER".equals(type)) {
			if (this.getColumnName().toLowerCase().contains("id")) {
				return "NUMERIC";
			} else {
				return "INTEGER";
			}
		} else if ("TIMESTAMP(6)".equals(type)) {
			return "TIMESTAMP";
		} else if ("CHAR".equals(type)) {
			return "CHAR";
		}
		return "VARCHAR";
	}

	/**
	 * 得到JAVA代码里面对应的字段类型的方法
	 *
	 * @param type
	 * @return String
	 */
	private String getFType(String type) {
		if ("VARCHAR".equals(type)) {
			return "String";
		} else if ("NUMERIC".equals(type)) {
			return "Long";
		} else if ("TIME".equals(type)) {
			return "Date";
		} else if ("CHAR".equals(type)) {
			return "String";
		} else if ("INTEGER".equals(type)) {
			return "int";
		}
		return "String";
	}

	@Override
	public String toString() {
		return "GenerateCodeModule [tableName=" + tableName + ", propertyName=" + propertyName + ", columnName="
				+ columnName + ", jdbcType=" + jdbcType + ", fieldDis=" + fieldDis + ", capFieldName=" + capFieldName
				+ ", fieldType=" + fieldType + "]";
	}

}
